package com.vancouver.knows.business.tracker;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pangu.bean.PageableResult;
import com.pangu.task.IResultReceiver;
import com.pangu.task.OperateResult;
import com.pangu.task.mark.ATaskMark;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.Constants;
import com.vancouver.knows.business.bean.EventBannerInfo;
import com.vancouver.knows.business.bean.EventInfo;
import com.vancouver.knows.business.service.impl.FetchEventsHandler;

/**
 * 获取活动列表
 * 
 * @author chenxunlin01
 * 
 */
public class FetchEventsTracker extends ABInvokeTracker {

	public FetchEventsTracker(BusinessModule module, IResultReceiver resultReceiver) {
		super(module, resultReceiver);
	}

	@Override
	protected void handleTaskRefresh(ATaskMark taskMark, OperateResult result) {
		module.getCacheManager().getEventInfoHashMap().clear();
	}

	@Override
	public String TAG() {
		return FetchEventsTracker.class.getSimpleName();
	}

	@Override
	public void handleResult(OperateResult result) {
		PageableResult pageableResult = (PageableResult) result.getResultData();
		if (pageableResult.getContent() != null) {
			Map<String, Object> map = (Map<String, Object>) pageableResult.getContent();
			HashMap<String,List<EventInfo>> eventInfoHashMap= (HashMap<String, List<EventInfo>>) map.get(FetchEventsHandler.KEY_POSTS);
			module.getCacheManager().setEventInfoHashMap(eventInfoHashMap);
			List<EventBannerInfo> eventBannerInfoList = (List<EventBannerInfo>) map
					.get(FetchEventsHandler.KEY_HEAD_POSTS);
			if (eventBannerInfoList != null && eventBannerInfoList.size() > 0) {
				module.getCacheManager().getEventBannerInfoList().clear();
				module.getCacheManager().getEventBannerInfoList().addAll(eventBannerInfoList);
			}

			List<String> allDates = (List<String>) map.get(FetchEventsHandler.KEY_ALL_DATES);
			module.getCacheManager().setAllDates(allDates);

			HashMap<String,List<EventInfo>> dateHashMap = (HashMap<String, List<EventInfo>>) map.get(FetchEventsHandler.KEY_DATE);
			module.getCacheManager().setDateInfoHashMap(dateHashMap);

			imContext.handleMobEmptyMessage(Constants.BUSINESS_STATUS_EVENT_LIST_HEAD_POSTS_CHANGE);
			imContext.handleMobEmptyMessage(Constants.BUSINESS_STATUS_EVENT_LIST_CHANGE);
		}
	}
}
